<?php
#BEGIN_LICENSE
#-------------------------------------------------------------------------
# Module: CGBlog (c) 2010 by Robert Campbell 
#         (calguy1000@cmsmadesimple.org)
#  An addon module for CMS Made Simple to allow creation, management of
#  and display of blog articles.
# 
#-------------------------------------------------------------------------
# CMS - CMS Made Simple is (c) 2005 by Ted Kulp (wishy@cmsmadesimple.org)
# This project's homepage is: http://www.cmsmadesimple.org
#
#-------------------------------------------------------------------------
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# However, as a special exception to the GPL, this software is distributed
# as an addon module to CMS Made Simple.  You may not use this software
# in any Non GPL version of CMS Made simple, or in any version of CMS
# Made simple that does not indicate clearly and obviously in its admin 
# section that the site was built with CMS Made simple.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Or read it online: http://www.gnu.org/licenses/licenses.html#GPL
#
#-------------------------------------------------------------------------
#END_LICENSE

final class cgblog_utils
{
  private function __construct() {}

  private static function coalesce($data,$key,$dflt = '')
  {
    if( isset($data[$key]) ) {
      return $data[$key];
    }
    return $dflt;
  }


  public static function handle_uploaded_file($id,$itemid,$fieldid,$attrs,&$error,$prefix = 'customfield_')
  {
    $gCms = cmsms();
    $config = $gCms->GetConfig();

    $dir = cms_join_path($config['uploads_path'],'cgblog');
    $res = cge_dir::mkdirr($dir);
    $mod = cge_utils::get_module('CGBlog');
    if( !$res ) {
      $error = $mod->Lang('error_mkdir').': '.$dir;
      return FALSE;
    }

    $dir = cms_join_path($dir,'id'.$itemid);
    $res = cge_dir::mkdirr($dir);
    if( !$res ) {
      $error = $mod->Lang('error_mkdir').': '.$dir;
      return FALSE;
    }
    
    $uploader = cge_setup::get_uploader('',$dir);    
    $uploader->set_accepted_filetypes(self::coalesce($attrs,'file_exts'));

    $name = $id.$prefix.$fieldid;
    $res = $uploader->handle_upload($name);
    $err = $uploader->get_error();
    if( !$res && $err != cg_fileupload::NOFILE ) {
      $error = $mod->GetUploadErrorMessage($err);
      return FALSE;
    }
    return $res;
  }


  public static function handle_uploaded_image($id,$itemid,$fieldid,$attrs,&$error,$prefix = 'customfield_')
  {
    global $gCms;
    $config = $gCms->GetConfig();

    $dir = cms_join_path($config['uploads_path'],'cgblog');
    $res = cge_dir::mkdirr($dir);
    if( !$res ) return FALSE;

    $dir = cms_join_path($dir,'id'.$itemid);
    $res = cge_dir::mkdirr($dir);
    if( !$res ) return FALSE;
    
    $uploader = cge_setup::get_uploader('',$dir);    
    $uploader->set_accepted_imagetypes(self::coalesce($attrs,'image_exts'));
    
    $name = $id.$prefix.$fieldid;
    $res = $uploader->handle_upload($name);
    $err = $uploader->get_error();
    if( !$res && $err != cg_fileupload::NOFILE ) {
      $mod = cge_utils::get_module('CGBlog');
      $error = $mod->GetUploadErrorMessage($err);
      return FALSE;
    }
    return $res;
  }

  public static function get_detailpage($current_page,$params)
  {
    $mod = cms_utils::get_module('CGBlog');
    $gCms = cmsms();
    $detailpage = $current_page;
    $tmp = $mod->GetPreference('default_detailpage',-1);
    if( $tmp != -1 && $tmp != '') {
      $detailpage = $tmp;
    }
    if (isset($params['detailpage'])) {
      $tmp = $mod->resolve_alias_or_id($params['detailpage']);
      if( !empty($tmp) ) {
	$detailpage = $tmp;
      }
    }
    if( $detailpage <= 0 ) {
      $detailpage = $gCms->GetContentOperations()->GetDefaultContent();
    }

    return $detailpage;
  }

} // end of class

?>